package gr.unipi.mscd1;

import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;

import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.border.LineBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.JScrollPane;
import java.awt.FlowLayout;
import javax.swing.JTextArea;

public class HostResForm extends JFrame {
	
	private SimpleSnmpClient client;
	HostResForm thisfrm;
	private JPanel pnlProcesses;
	private JPanel pnlStorage;
	private JPanel pnlDevices;
	private JTable tblDevices;
	private JTable tblStorage;
	private JTable tblProcess;
	private JPanel pnlDevicesTitle;
	private JLabel lblDevicesTitle;
	private JScrollPane scrollPane;
	private JPanel pnlStorageTitle;
	private JLabel lblStorageTitle;
	private JPanel pnlProcessesTitle;
	private JLabel lblProcessesTitle;
	private JScrollPane scrollPane_1;
	private JPanel pnlStorageMain;
	private JPanel pnlStorageRam;
	private JLabel lblRam;
	private JTextArea txtRam;
	private JScrollPane scrollPane_2;
	
	private DeviceTableModel devModel;
	private StorageTableModel storeModel;
	private ProcessTableModel procModel;
	
	public HostResForm(SimpleSnmpClient client) {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("HOST_RESOURCES");
		thisfrm = this;
		this.client = client;
		getContentPane().setBackground(Color.WHITE);
		getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		
		pnlDevices = new JPanel();
		pnlDevices.setBackground(Color.WHITE);
		getContentPane().add(pnlDevices);
		pnlDevices.setLayout(new BorderLayout(0, 0));
		
		pnlDevicesTitle = new JPanel();
		pnlDevicesTitle.setBorder(new LineBorder(new Color(0, 0, 0)));
		pnlDevicesTitle.setBackground(Color.WHITE);
		pnlDevices.add(pnlDevicesTitle, BorderLayout.NORTH);
		
		lblDevicesTitle = new JLabel("Devices");
		lblDevicesTitle.setFont(new Font("Tahoma", Font.BOLD, 18));
		pnlDevicesTitle.add(lblDevicesTitle);
		
		tblDevices = new JTable();
		tblDevices.setBackground(Color.WHITE);
		//tblDevices.getSelectionModel().addListSelectionListener(this);
		
		
		
		scrollPane = new JScrollPane(tblDevices);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane.setBackground(Color.WHITE);
		scrollPane.getViewport().setBackground(Color.WHITE);
		pnlDevices.add(scrollPane, BorderLayout.CENTER);
		
		pnlStorage = new JPanel();
		pnlStorage.setBackground(Color.WHITE);
		getContentPane().add(pnlStorage);
		pnlStorage.setLayout(new BorderLayout(0, 0));
		
		pnlStorageTitle = new JPanel();
		pnlStorageTitle.setBorder(new LineBorder(new Color(0, 0, 0)));
		pnlStorageTitle.setBackground(Color.WHITE);
		pnlStorage.add(pnlStorageTitle, BorderLayout.NORTH);
		
		lblStorageTitle = new JLabel("Storage");
		lblStorageTitle.setFont(new Font("Tahoma", Font.BOLD, 18));
		pnlStorageTitle.add(lblStorageTitle);
		
		pnlStorageMain = new JPanel();
		pnlStorageMain.setBackground(Color.WHITE);
		pnlStorage.add(pnlStorageMain, BorderLayout.CENTER);
		pnlStorageMain.setLayout(new BorderLayout(0, 0));
		
		pnlStorageRam = new JPanel();
		FlowLayout flowLayout = (FlowLayout) pnlStorageRam.getLayout();
		flowLayout.setAlignment(FlowLayout.LEADING);
		pnlStorageRam.setBackground(Color.WHITE);
		pnlStorageMain.add(pnlStorageRam, BorderLayout.NORTH);
		
		lblRam = new JLabel("Ram size (MB):");
		pnlStorageRam.add(lblRam);
		
		txtRam = new JTextArea();
		txtRam.setColumns(5);
		txtRam.setEditable(false);
		pnlStorageRam.add(txtRam);
		
		tblStorage = new JTable();
		tblStorage.setBackground(Color.WHITE);
		
		scrollPane_2 = new JScrollPane(tblStorage);
		scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane_2.setBackground(Color.WHITE);
		scrollPane_2.getViewport().setBackground(Color.WHITE);
		pnlStorageMain.add(scrollPane_2, BorderLayout.CENTER);
		
		pnlProcesses = new JPanel();
		pnlProcesses.setBackground(Color.WHITE);
		getContentPane().add(pnlProcesses);
		pnlProcesses.setLayout(new BorderLayout(0, 0));
		
		pnlProcessesTitle = new JPanel();
		pnlProcessesTitle.setBorder(new LineBorder(new Color(0, 0, 0)));
		pnlProcessesTitle.setBackground(Color.WHITE);
		pnlProcesses.add(pnlProcessesTitle, BorderLayout.NORTH);
		
		lblProcessesTitle = new JLabel("Processes");
		lblProcessesTitle.setFont(new Font("Tahoma", Font.BOLD, 18));
		pnlProcessesTitle.add(lblProcessesTitle);
		
		tblProcess = new JTable();
		tblProcess.setBackground(Color.WHITE);
		
		scrollPane_1 = new JScrollPane(tblProcess);
		pnlProcesses.add(scrollPane_1, BorderLayout.CENTER);
		scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane_1.setBackground(Color.WHITE);
		scrollPane_1.getViewport().setBackground(Color.WHITE);
		
		fillDeviceTable();
		fillStorageTable();
		fillProcessTable();
		
		setBounds(50, 50, 800, 600);
		setVisible(true);
	}
	
	private void fillDeviceTable() {
		devModel = new DeviceTableModel(client);
		tblDevices.setModel(devModel);
		tblDevices.setAutoCreateRowSorter(true);
		tblDevices.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tblDevices.getColumnModel().getColumn(0).setPreferredWidth(50);
		tblDevices.getColumnModel().getColumn(1).setPreferredWidth(500);
		tblDevices.getColumnModel().getColumn(2).setPreferredWidth(100);
		tblDevices.getColumnModel().getColumn(3).setPreferredWidth(100);
	}
	
	private void fillStorageTable() {
		RamSize ram = new RamSize(client, txtRam);
		ram.start();
		
		storeModel = new StorageTableModel(client);
		tblStorage.setModel(storeModel);
		tblStorage.setAutoCreateRowSorter(true);
		tblStorage.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tblStorage.getColumnModel().getColumn(0).setPreferredWidth(50);
		tblStorage.getColumnModel().getColumn(1).setPreferredWidth(430);
		tblStorage.getColumnModel().getColumn(2).setPreferredWidth(120);
		tblStorage.getColumnModel().getColumn(3).setPreferredWidth(150);
	}
	
	private void fillProcessTable() {
		procModel = new ProcessTableModel(client);
		
		tblProcess.setModel(procModel);
		tblProcess.setAutoCreateRowSorter(true);
		tblProcess.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tblProcess.getColumnModel().getColumn(0).setPreferredWidth(50);
		tblProcess.getColumnModel().getColumn(1).setPreferredWidth(430);
		tblProcess.getColumnModel().getColumn(2).setPreferredWidth(120);
		tblProcess.getColumnModel().getColumn(3).setPreferredWidth(150);
	}
	
	public static void open(SimpleSnmpClient client) {
		new HostResForm(client);
	}
}
